package com.konsung.util;

import com.konsung.bean.MeasureDataBean;
import com.konsung.bean.PhysicalExaminationBean;
import com.konsung.healthfile.R;
import com.konsung.provide.ProviderReader;
import com.konsung.util.constant.GlobalConstant;

import java.util.Locale;

import static com.konsung.util.UiUtils.getContent;
import static com.konsung.util.UiUtils.getFactor;
import static com.konsung.util.UiUtils.getString;
import static com.konsung.util.UiUtils.getStringArr;

/**
 * 类功能： 测量数据装换的工具类
 */

public class MeasureUtil {

    /**
     * 根据测量数据和KParamType获取相对应的数据
     * @param param KParamType
     * @param measureBean 测量数据
     * @return PhysicalExaminationBean
     */
    public static PhysicalExaminationBean getPhysicalExaminationBean(int param,
            MeasureDataBean measureBean) {
        if (measureBean != null) {
            boolean isU120 = false;
            int paramValue = measureBean.getParamValue();
            if ((paramValue & (0x01 << 10)) != 0) {
                isU120 = true;
            }
            PhysicalExaminationBean physicalBean = new PhysicalExaminationBean();
            physicalBean.setMeasureValue(measureValueShowUi(param, measureBean.
                    getTrendValue(param)));
            physicalBean.setMeasureName(getParamString(param, isU120));
            physicalBean.setTitleName(getTitleName(param));
            physicalBean.setUnit(getValueUnit(param));
            physicalBean.setScope(ReferenceUtils.getReferenceRange(param));
            int flag = ReferenceUtils.compareWithReference(param, measureBean.getTrendValue(param));
            if (flag == ReferenceUtils.FLAG_VALUE_ABOVE) {
                physicalBean.setHint(UiUtils.getString(R.string.above));
            } else if (flag == ReferenceUtils.FLAG_VALUE_BELOW) {
                physicalBean.setHint(UiUtils.getString(R.string.below));
            } else {
                physicalBean.setHint("");
            }
            return physicalBean;
        }
        return null;
    }

    /**
     * 获取转换后的测量值用于界面显示(尿常规需要显示+-，尿酸总胆固醇需要保留小数，血压心率需要整数)
     * @param param 参数
     * @param value 结果
     * @return 转换后的测量值
     */
    public static String measureValueShowUi2(int param, int value) {
        if (value != GlobalConstant.INVALID_DATA) {
            switch (param) {
                case KParamType.ECG_HR:
                case KParamType.SPO2_PR:
                case KParamType.SPO2_TREND:
                case KParamType.NIBP_SYS:
                case KParamType.NIBP_DIA:
                case KParamType.NIBP_PR:
                case KParamType.NIBP_MAP:
                case KParamType.RESP_RR:
                case KParamType.BLOOD_HGB:
                case KParamType.BLOOD_HCT:
                    return value / GlobalConstant.INT_FACTOR + "";
                case KParamType.URINERT_BIL:
                case KParamType.URINERT_BLD:
                case KParamType.URINERT_GLU:
                case KParamType.URINERT_KET:
                case KParamType.URINERT_LEU:
                case KParamType.URINERT_NIT:
                case KParamType.URINERT_PRO:
                case KParamType.URINERT_UBG:
                case KParamType.URINERT_ASC:
                    return urtValueToString(value);
                case KParamType.URINERT_ALB:
                case KParamType.URINERT_CRE:
                case KParamType.URINERT_CA:
                    return getUrtSpecialValue(param, value);
                case KParamType.URINERT_AC:
                    return getU60ACValue(value);

                case KParamType.BLOOD_WBC:
                    return value / GlobalConstant.WBC_FACTOR + "";
                case KParamType.BLOODGLU_AFTER_MEAL:
                case KParamType.BLOODGLU_BEFORE_MEAL:
                case KParamType.URICACID_TREND:
                case KParamType.URINERT_PH:
                case KParamType.CHOLESTEROL_TREND:
                case KParamType.WEIGHT:
                case KParamType.HEIGHT:
                case KParamType.TEMP_T1:
                case KParamType.IRTEMP_TREND:
                    return value / GlobalConstant.FACTOR + "";
                case KParamType.URINERT_SG:
                    return String.format(Locale.ENGLISH,
                            getString(R.string.rule_limit_3_after_point),
                            value / GlobalConstant.SG_FACTOR);
                case KParamType.GHB_EAG:
                case KParamType.GHB_HBA1C_NGSP:
                case KParamType.GHB_HBA1C_IFCC:
                case KParamType.BLOOD_FAT_CHO:
                case KParamType.BLOOD_FAT_HDL:
                case KParamType.BLOOD_FAT_LDL:
                case KParamType.BLOOD_FAT_TRIG:
                    if (value == OverCheckUtil.FLAG_OVER_MAX) {
                        return OverCheckUtil.getOverMaxString(param, value);
                    } else if (value == OverCheckUtil.FLAG_BELOW_MIN) {
                        return OverCheckUtil.getOverMinString(param, value);
                    } else {
                        return String.valueOf(value / GlobalConstant.FACTOR);
                    }
                case KParamType.BMI:
                    return String.valueOf(value / GlobalConstant.FACTOR);
                default:
                    return "";
            }
        }

        return "";
    }
    /**
     * 获取转换后的测量值用于界面显示(尿常规需要显示+-，尿酸总胆固醇需要保留小数，血压心率需要整数)
     * @param param 参数
     * @param value 结果
     * @return 转换后的测量值
     */
    public static String measureValueShowUi(int param, int value) {
        if (value != GlobalConstant.INVALID_DATA) {
            switch (param) {
                case KParamType.ECG_HR:
                case KParamType.SPO2_PR:
                case KParamType.SPO2_TREND:
                case KParamType.NIBP_SYS:
                case KParamType.NIBP_DIA:
                case KParamType.NIBP_PR:
                case KParamType.NIBP_MAP:
                case KParamType.RESP_RR:
                case KParamType.BLOOD_HGB:
                case KParamType.BLOOD_HCT:
                    return value / GlobalConstant.INT_FACTOR + "";
                case KParamType.URINERT_BIL:
                case KParamType.URINERT_BLD:
                case KParamType.URINERT_GLU:
                case KParamType.URINERT_KET:
                case KParamType.URINERT_LEU:
                case KParamType.URINERT_NIT:
                case KParamType.URINERT_PRO:
                case KParamType.URINERT_UBG:
                case KParamType.URINERT_ASC:
                    return urtValueToString(value);
                case KParamType.URINERT_ALB:
                case KParamType.URINERT_CRE:
                case KParamType.URINERT_CA:
                    return getUrtSpecialValue(param, value);
                case KParamType.URINERT_AC:
                    return getU60ACValue(value);

                case KParamType.BLOOD_WBC:
                    return value / GlobalConstant.WBC_FACTOR + "";
                case KParamType.BLOODGLU_AFTER_MEAL:
                case KParamType.BLOODGLU_BEFORE_MEAL:
                case KParamType.URICACID_TREND:
                case KParamType.URINERT_PH:
                case KParamType.CHOLESTEROL_TREND:
                case KParamType.WEIGHT:
                case KParamType.HEIGHT:
                case KParamType.TEMP_T1:
                case KParamType.IRTEMP_TREND:
                    return value / GlobalConstant.FACTOR + "";
                case KParamType.URINERT_SG:
                    return String.format(Locale.ENGLISH,
                            getString(R.string.rule_limit_3_after_point),
                            value / GlobalConstant.SG_FACTOR);
                case KParamType.GHB_EAG:
                case KParamType.GHB_HBA1C_NGSP:
                case KParamType.GHB_HBA1C_IFCC:
                case KParamType.BLOOD_FAT_CHO:
                case KParamType.BLOOD_FAT_HDL:
                case KParamType.BLOOD_FAT_LDL:
                case KParamType.BLOOD_FAT_TRIG:
                    if (value == OverCheckUtil.FLAG_OVER_MAX) {
                        return OverCheckUtil.getOverMaxString(param, value);
                    } else if (value == OverCheckUtil.FLAG_BELOW_MIN) {
                        return OverCheckUtil.getOverMinString(param, value);
                    } else {
                        return String.valueOf(value / GlobalConstant.FACTOR);
                    }
                case KParamType.BMI:
                    return String.valueOf(value / GlobalConstant.FACTOR);
                default:
                    return getString(R.string.invalid_data);
            }
        }

        return getString(R.string.invalid_data);
    }
    /**
     * U60 A:C值数据
     * @param value 值
     * @return 显示值
     */
    private static String getU60ACValue(int value) {
        switch (value) {
            case ReferenceUtils.URT_AC_NORMAL:
                return getString(R.string.u60_ac_value_normal);
            case ReferenceUtils.URT_AC_ABNORMAL:
                return getString(R.string.u60_ac_value_abnormal);
            case ReferenceUtils.URT_AC_HIGH_NORMAL:
                return getString(R.string.u60_ac_value_high_abnormal);
            default:
                return String.valueOf(value);
        }
    }

    /**
     * 艾康U120尿常规肌酐，尿钙，微量白蛋白 三项显示值
     * @param param 参数
     * @param value 值
     * @return 需要显示的尿常规数值
     */
    public static String getUrtSpecialValue(int param, int value) {
        String[] valueString;
        //艾康U120
        if (getAiKangUseStatus()) {
            switch (param) {
                case KParamType.URINERT_ALB:
                    if (getU120OUsing()) {
                        valueString = getStringArr(R.array.alb_value_u120);
                    } else {
                        valueString = getStringArr(R.array.alb_value_u60);
                    }
                    break;
                case KParamType.URINERT_CRE:
                    if (getU120OUsing()) {
                        valueString = getStringArr(R.array.cre_value_u120);
                    } else {
                        valueString = getStringArr(R.array.cre_value_u60);
                    }
                    break;
                case KParamType.URINERT_CA:
                    valueString = getStringArr(R.array.ca_value_u120);
                    break;
                default:
                    return String.valueOf(value / getFactor(param));
            }
            value = value / GlobalConstant.INT_FACTOR;
            switch (value) {
                case -1:
                    return valueString[0];
                case 1:
                    return valueString[1];
                case 2:
                    return valueString[2];
                case 3:
                    return valueString[3];
                case 4:
                    //微量白蛋白没有+4
                    if (param == KParamType.URINERT_ALB) {
                        return String.valueOf(value);
                    } else {
                        return valueString[4];
                    }
                default:
                    return String.valueOf(value);
            }
        } else {
            return urtValueToString(value);
        }
    }

    /**
     * 获取是否艾康U120在使用
     * @return true -U120, false -U60
     */
    public static boolean getU120OUsing() {
        int config = ProviderReader.getUrtSetting(getContent());
        if ((config & (0x01 << 1)) != 0) {
            //第一位11+2项
            return false;
        } else {
            //其它默认显示14项
            return true;
        }
    }

    /**
     * 尿常规值转换
     * @param value 模块传递过来的值
     * @return 显示测量值
     */
    public static String urtValueToString(int value) {
        if (value == GlobalConstant.INVALID_DATA) {
            return getString(R.string.invalid_data);
        }

        value = value / GlobalConstant.INT_FACTOR;
        final String[] cas;
        if (getAiKangUseStatus()) {

            cas = getStringArr(R.array.uri_value_u120);
        } else {
            cas = getStringArr(R.array.uri_value);
        }
        switch (value) {
            case -1:
                return cas[0];
            case 0:
                return cas[1];
            case 1:
                return cas[2];
            case 2:
                return cas[3];
            case 3:
                return cas[4];
            case 4:
                return cas[5];
            case 5:
                return cas[6];
            case 6:
                return cas[7];
            case 7:
                return cas[8];
            case 8:
                return cas[9];
            case 9:
                return cas[10];
            case 10:
                return cas[11];
            case 11:
                return cas[12];
            case 12:
                return cas[13];
            default:
                return String.valueOf(value);
        }
    }

    /**
     * 返回参数显示的值的字符串
     * @param param 参数
     * @param value 原始值
     * @return 比例
     */
    public static String getValueAfterFactor(int param, int value) {
        if (value == GlobalConstant.INVALID_DATA) {
            return UiUtils.getString(R.string.invalid_data);
        }
        switch (param) {
            case KParamType.GHB_EAG:
            case KParamType.GHB_HBA1C_NGSP:
            case KParamType.GHB_HBA1C_IFCC:
                if (value == OverCheckUtil.FLAG_OVER_MAX) {
                    return OverCheckUtil.getOverMaxString(param, value);
                } else if (value == OverCheckUtil.FLAG_BELOW_MIN) {
                    return OverCheckUtil.getOverMinString(param, value);
                } else {
                    return String.valueOf(value / GlobalConstant.FACTOR);
                }
            case KParamType.BLOOD_FAT_TRIG:
            case KParamType.BLOOD_FAT_CHO:
            case KParamType.BLOOD_FAT_HDL:
            case KParamType.BLOOD_FAT_LDL:
                if (value == OverCheckUtil.FLAG_OVER_MAX) {
                    return OverCheckUtil.getOverMaxString(param, value);
                } else if (value == OverCheckUtil.FLAG_BELOW_MIN) {
                    return OverCheckUtil.getOverMinString(param, value);
                } else {
                    return String.format(Locale.ENGLISH, getString(R.string
                                    .rule_limit_2_after_point),
                            value / GlobalConstant.FACTOR);
                }
                //下面这几项，都是整数
            case KParamType.ECG_HR:
            case KParamType.SPO2_PR:
            case KParamType.SPO2_TREND:
            case KParamType.NIBP_DIA:
            case KParamType.NIBP_SYS:
            case KParamType.BLOOD_HCT:
                return String.valueOf(value / GlobalConstant.INT_FACTOR);
            default:
                return String.valueOf(value / getFactor(param));
        }
    }

    /**
     * 根据参数值获取单位字符串
     * @param param 参数
     * @return 单位
     */
    public static String getValueUnit(int param) {
        switch (param) {
            case KParamType.ECG_HR:
            case KParamType.SPO2_PR:
                return getString(R.string.health_unit_bpm);

            case KParamType.NIBP_SYS:
            case KParamType.NIBP_DIA:
                return getString(R.string.unit_mmhg);

            case KParamType.TEMP_T1:
            case KParamType.IRTEMP_TREND:
                return getString(R.string.unit_temp);

            case KParamType.BLOOD_HCT:
            case KParamType.SPO2_TREND:
            case KParamType.GHB_HBA1C_NGSP:
                return getString(R.string.unit_percent);

            case KParamType.BLOODGLU_AFTER_MEAL:
            case KParamType.BLOODGLU_BEFORE_MEAL:
            case KParamType.URICACID_TREND:
            case KParamType.CHOLESTEROL_TREND:
            case KParamType.BLOOD_FAT_CHO:
            case KParamType.BLOOD_FAT_HDL:
            case KParamType.BLOOD_FAT_LDL:
            case KParamType.BLOOD_FAT_TRIG:
                return getString(R.string.unit_mmol_l);

            case KParamType.GHB_HBA1C_IFCC:
                return getString(R.string.unit_mmol_mol);

            case KParamType.BLOOD_HGB:
                return getString(R.string.unit_gl);
            case KParamType.BLOOD_WBC:
                return getString(R.string.unit_index);
            case KParamType.GHB_EAG:
            case KParamType.URINERT_CA:
            case KParamType.URINERT_CRE:
                return getString(R.string.unit_mg_dl);
            case KParamType.WEIGHT:
                return getString(R.string.unit_kg);
            case KParamType.HEIGHT:
                return getString(R.string.unit_cm);
            case KParamType.URINERT_ALB:
                return getString(R.string.unit_mg_l);
            case KParamType.URINERT_AC:
                return getString(R.string.unit_mg_g);
            default:
                return "/";
        }
    }

    /**
     * 根据参数值获取测量项名称
     * @param param 参数
     * @param isU120 是否u120设备
     * @return 名称
     */
    public static String getParamString(int param, boolean isU120) {
        switch (param) {
            case KParamType.ECG_HR:
                return UiUtils.getString(R.string.ecg_hr);
            case KParamType.SPO2_TREND:
                return UiUtils.getString(R.string.spo2);
            case KParamType.SPO2_PR:
                return UiUtils.getString(R.string.pr_cn);
            case KParamType.NIBP_SYS:
                return UiUtils.getString(R.string.nibp_sys);
            case KParamType.NIBP_DIA:
                return UiUtils.getString(R.string.nibp_dia);
            case KParamType.IRTEMP_TREND:
                return UiUtils.getString(R.string.temp);
            case KParamType.BLOOD_HCT:
                return UiUtils.getString(R.string.red_blood_cells_backlog_value);
            case KParamType.BLOOD_HGB:
                return UiUtils.getString(R.string.hb);
            case KParamType.BLOODGLU_AFTER_MEAL:
                return UiUtils.getString(R.string.after_glu);
            case KParamType.BLOODGLU_BEFORE_MEAL:
                return UiUtils.getString(R.string.before_glu);
            case KParamType.URICACID_TREND:
                return UiUtils.getString(R.string.ua);

            case KParamType.CHOLESTEROL_TREND:
                return UiUtils.getString(R.string.total_cho);

            case KParamType.BLOOD_FAT_CHO:
                return UiUtils.getString(R.string.total_cho);

            case KParamType.BLOOD_FAT_HDL:
                return UiUtils.getString(R.string.hdl);

            case KParamType.BLOOD_FAT_LDL:
                return UiUtils.getString(R.string.ldl);

            case KParamType.BLOOD_FAT_TRIG:
                return UiUtils.getString(R.string.trig);
            case KParamType.GHB_HBA1C_NGSP:
                return UiUtils.getString(R.string.ghb_ngsp);
            case KParamType.GHB_HBA1C_IFCC:
                return UiUtils.getString(R.string.ghb_ifcc);
            case KParamType.GHB_EAG:
                return UiUtils.getString(R.string.ghb_eag);
            case KParamType.BLOOD_WBC:
                return UiUtils.getString(R.string.wbc);
            case KParamType.HEIGHT:
                return UiUtils.getString(R.string.height);
            case KParamType.WEIGHT:
                return UiUtils.getString(R.string.weight);
            case KParamType.URINERT_LEU:
                return UiUtils.getString(R.string.urt_name_leu);
            case KParamType.URINERT_NIT:
                return UiUtils.getString(R.string.urt_name_nit);
            case KParamType.URINERT_UBG:
                return UiUtils.getString(R.string.urt_name_ubg);
            case KParamType.URINERT_PRO:
                return UiUtils.getString(R.string.urt_name_pro);
            case KParamType.URINERT_PH:
                return UiUtils.getString(R.string.urt_name_ph);
            case KParamType.URINERT_BLD:
                if (isU120) {
                    return UiUtils.getString(R.string.urt_name_bld2);
                } else {
                    return UiUtils.getString(R.string.urt_name_bld);
                }
            case KParamType.URINERT_SG:
                return UiUtils.getString(R.string.urt_name_sg);
            case KParamType.URINERT_BIL:
                return UiUtils.getString(R.string.urt_name_bil);
            case KParamType.URINERT_KET:
                return UiUtils.getString(R.string.urt_name_ket);
            case KParamType.URINERT_GLU:
                return UiUtils.getString(R.string.urt_name_glu);
            case KParamType.URINERT_ALB:
                return UiUtils.getString(R.string.urt_name_ma);
            case KParamType.URINERT_VC:
            case KParamType.URINERT_ASC:
                if (isU120) {
                    return UiUtils.getString(R.string.urt_name_asc);
                } else {
                    return UiUtils.getString(R.string.urt_name_vc);
                }
            case KParamType.URINERT_CRE:
                return UiUtils.getString(R.string.urt_name_cr);
            case KParamType.URINERT_CA:
                if (isU120) {
                    return UiUtils.getString(R.string.urt_name_ca_u120);
                } else {
                    return UiUtils.getString(R.string.urt_name_ca);
                }
            case KParamType.URINERT_AC:
                return UiUtils.getString(R.string.urt_name_ac);
            case KParamType.BMI:
                return UiUtils.getString(R.string.bmi);
            default:
                return "";
        }
    }

    /**
     * 根据参数值获取测量项名称
     * @param param 参数
     * @return 名称
     */
    public static String getTitleName(int param) {
        switch (param) {
            case KParamType.ECG_HR:
                return UiUtils.getString(R.string.ecg_hr);
            case KParamType.SPO2_TREND:
                return UiUtils.getString(R.string.spo2);
            case KParamType.SPO2_PR:
                return UiUtils.getString(R.string.pr_cn);
            case KParamType.NIBP_SYS:
                return UiUtils.getString(R.string.bp);
            case KParamType.NIBP_DIA:
                return UiUtils.getString(R.string.bp);
            case KParamType.IRTEMP_TREND:
                return UiUtils.getString(R.string.temp);
            case KParamType.BLOOD_HCT:
                return UiUtils.getString(R.string.red_blood_cells_backlog_value);
            case KParamType.URINERT_LEU:
                return UiUtils.getString(R.string.urine);
            case KParamType.BLOOD_HGB:
                return UiUtils.getString(R.string.hb);
            case KParamType.BLOODGLU_AFTER_MEAL:
                return UiUtils.getString(R.string.glu);
            case KParamType.BLOODGLU_BEFORE_MEAL:
                return UiUtils.getString(R.string.glu);
            case KParamType.URICACID_TREND:
                return UiUtils.getString(R.string.ua);

            case KParamType.CHOLESTEROL_TREND:
                return UiUtils.getString(R.string.total_cho);

            case KParamType.BLOOD_FAT_CHO:
                return UiUtils.getString(R.string.blood_fat);

            case KParamType.BLOOD_FAT_HDL:
                return UiUtils.getString(R.string.hdl);

            case KParamType.BLOOD_FAT_LDL:
                return UiUtils.getString(R.string.ldl);

            case KParamType.BLOOD_FAT_TRIG:
                return UiUtils.getString(R.string.trig);
            case KParamType.GHB_HBA1C_NGSP:
                return UiUtils.getString(R.string.hba1c);
            case KParamType.GHB_HBA1C_IFCC:
                return UiUtils.getString(R.string.ghb_ifcc);
            case KParamType.GHB_EAG:
                return UiUtils.getString(R.string.ghb_eag);
            case KParamType.BLOOD_WBC:
                return UiUtils.getString(R.string.wbc);
            case KParamType.HEIGHT:
                return UiUtils.getString(R.string.height);
            case KParamType.WEIGHT:
                return UiUtils.getString(R.string.weight);
            case KParamType.BMI:
                return UiUtils.getString(R.string.bmi);
            default:
                return "";
        }
    }

    /**
     * 获取艾康U120使用情况
     * @return 是否使用U120
     */
    public static boolean getAiKangUseStatus() {
        int config = ProviderReader.getDeviceConfig(getContent());
        if ((config & (0x01 << 10)) != 0) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 将测量数据已“测量项：测量值”返回，例如“心率:60bpm”
     * @param param param
     * @param value value
     * @return 例如“心率:60bpm”
     */
    public static String formatMeasureData(int param, int value) {
        String measureName = getTitleName(param);
        String measureValue = measureValueShowUi(param, value);
        String measureUnit = getValueUnit(param);
        if (measureUnit.equals(UiUtils.getString(R.string.unit_divide))) {
            return measureName + UiUtils.getString(R.string.colon) + " " + measureValue;
        }
        return measureName + UiUtils.getString(R.string.colon) + " " + measureValue + measureUnit;
    }
}
